//
//  LPWKHasCommentAndHeadLineViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPWKHasCommentAndHeadLineViewController.h"
#import "STPhotoBroswer.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface LPWKHasCommentAndHeadLineViewController ()<UIWebViewDelegate>{
    UIView *_commentBGView;/** 评论页面背景View */
}
/**  wkwebView  */
@property (strong, nonatomic) UIWebView  *webView;

/**  显示view  */
@property (strong, nonatomic) STPhotoBroswer  *brower;

@end

@implementation LPWKHasCommentAndHeadLineViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.titlelabel.text = self.model.TITLE;
    [self.view addSubview:self.webView];
    
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.model.URL]]];
}

- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, navHeight, self.view.width, self.view.height - navHeight )];
        _webView.delegate = self;
        _webView.dataDetectorTypes = UIDataDetectorTypeAll;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView setOpaque:YES];
    }
    return _webView;
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [YJProgressHUD hide];
    //js代码
    NSString *jsString =
    @"(function(){"
    "var objs = document.getElementsByTagName('img');"
    "var imgReqs = '';"
    "for(var i=0,j=0;i<objs.length;i++)"
    "{"
        "var levelcode = objs[i].getAttribute('levelcode');"
        "var path = objs[i].getAttribute('src');"
        "if(levelcode=='-1'){"
            "continue;"
        "}"
        "if(imgReqs){imgReqs += (','+path);}"
        "else imgReqs = path;"
        "objs[i].setAttribute('index',j);"
        "j++;"
        "objs[i].onclick=function()"
        "{"
            "var index = this.getAttribute('index');"
            "openImage(index,imgReqs);"
        "}"
    "}"
    "})()";
    
    [webView stringByEvaluatingJavaScriptFromString:jsString];
    //其中 openImage 是JS的函数名，得到的 arg数组 里面为JS的 passValue 函数的参数，即 JS要传给OC的参数。
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    context[@"openImage"] = ^(){
        NSMutableArray *images = [NSMutableArray array];
        NSArray *infor = [JSContext currentArguments];
        JSValue *index = infor.firstObject;
        JSValue *imageString = infor.lastObject;
        NSString *indexString = [index toString];
        NSString *string = [imageString toString];
        
        if ([string containsString:@","]) {
            NSArray *array = [string componentsSeparatedByString:@","];
            for (NSString *abcd in array) {
                
                [images addObject:[abcd stringByReplacingOccurrencesOfString:@"/iPB-GXB/" withString:@""]];
            }
            
        }else{
             [images addObject:[string stringByReplacingOccurrencesOfString:@"/iPB-GXB/" withString:@""]];
        }
        
         NSMutableArray *imageStirngs = [NSMutableArray array];
        NSString *firstImageString = [images objectAtIndex:0];
        if ([firstImageString containsString:@"http"]) {
            for (NSString *imageString in images) {
                NSMutableString *mutale = [[NSMutableString alloc] initWithString:imageString];
                
                NSString *getString = [mutale stringByReplacingOccurrencesOfString:BASEIMAGEURL withString:@""];
                
                [imageStirngs addObject:getString];
            }

        }else{
        
            for (NSString *imageString in images) {
//                NSString *string = [imageString substringFromIndex:11];
                
                [imageStirngs addObject:imageString];
            }
        }
       
        
        dispatch_async(dispatch_get_main_queue(), ^{
            STPhotoBroswer *view = [[STPhotoBroswer alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) ImageArray:imageStirngs currentIndex:[indexString integerValue]];
            [view show];
        });
    };

}


@end
